package com.mini.controller;

import com.mini.conditon.*;
import com.mini.config.BusinessException;
import com.mini.config.CheckLogin;
import com.mini.model.BusinessBelongingInfo;
import com.mini.model.BusinessGroupBase;
import com.mini.model.BusinessRangeInfo;
import com.mini.service.BusinessGroupManagerService;
import com.mini.vo.BusinessResult;
import com.mini.vo.CommonResultMessage;
import com.mini.vo.PageResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author: zhaocg
 * @created: 2020/12/03 11:47
 */
@Slf4j
@RestController
@RequestMapping(value = "/businessGroup")
@Api(tags = "业务组服务相关接口")
public class BusinessGroupManagerController {

    @Autowired
    private BusinessGroupManagerService groupManagerService;


    @CheckLogin
    @PostMapping(value = "/pageList")
    @ApiOperation(value = "业务组查询-分页" , notes = "业务组查询-分页")
    public BusinessResult<PageResultVO<BusinessGroupBase>> groupBasePageList(@ApiParam(value = "token", required = true) @RequestHeader String Authorization,
                                                                             @RequestBody GroupManagerQueryCondition condition){

        if (Objects.isNull(condition)) {
            throw new BusinessException("参数不合法");
        }
        if (condition.getUserId() == null) {
            throw new BusinessException("当前登录人ID为空");
        }

        return BusinessResult.createSuccessInstance(groupManagerService.groupBasePageList(condition));
    }

    @CheckLogin
    @PostMapping(value = "/save")
    @ApiOperation(value = "保存业务组数据" , notes = "保存业务组数据")
    public BusinessResult businessGroupSave(@ApiParam(value = "token", required = true) @RequestHeader String Authorization,
                                                               @RequestBody GroupManagerCondition condition){
        if (Objects.isNull(condition)) {
            throw new BusinessException("参数不合法");
        }
        if (condition.getOperatorId() == null) {
            throw new BusinessException("操作人为空");
        }
        if (StringUtils.isBlank(condition.getGroupName())) {
            throw new BusinessException("业务组名称为空");
        }
        if (CollectionUtils.isEmpty(condition.getChannelCodeList())) {
            throw new BusinessException("渠道为空");
        }
        if (CollectionUtils.isEmpty(condition.getRegionCodeList())) {
            throw new BusinessException("地域为空");
        }
        if (CollectionUtils.isEmpty(condition.getProjectIdList())) {
            throw new BusinessException("项目为空");
        }
        if (CollectionUtils.isEmpty(condition.getDeptIdList())) {
            throw new BusinessException("所属组为空");
        }
        if (condition.getLegionId() == null) {
            throw new BusinessException("量子军团为空");
        }
        if (condition.getArmyId() == null) {
            throw new BusinessException("军团为空");
        }
        groupManagerService.save(condition , Authorization);

        return BusinessResult.createInstance(CommonResultMessage.SUCCESS);
    }

    @CheckLogin
    @PostMapping(value = "/appendRangeInfo")
    @ApiOperation(value = "追加业务范围" , notes = "追加业务范围")
    public BusinessResult appendRangeInfo(@ApiParam(value = "token", required = true) @RequestHeader String Authorization,
                                          @RequestBody AppendRangeInfoCondition condition){
        if (Objects.isNull(condition)) {
            throw new BusinessException("参数不合法");
        }
        if (condition.getOperatorId() == null) {
            throw new BusinessException("操作人为空");
        }
        if (CollectionUtils.isEmpty(condition.getChannelCodeList())) {
            throw new BusinessException("渠道为空");
        }
        if (CollectionUtils.isEmpty(condition.getRegionCodeList())) {
            throw new BusinessException("地域为空");
        }
        if (CollectionUtils.isEmpty(condition.getProjectIdList())) {
            throw new BusinessException("项目为空");
        }
        if (condition.getBusinessGroupId() == null) {
            throw new BusinessException("业务组ID为空");
        }
        groupManagerService.appendRangeInfo(condition , Authorization);
        return BusinessResult.createInstance(CommonResultMessage.SUCCESS);
    }
    @CheckLogin
    @PostMapping(value = "/remakeBelonging")
    @ApiOperation(value = "重新编辑所属销售组" , notes = "重新编辑所属销售组")
    public BusinessResult deleteBelongingAndSave(@ApiParam(value = "token", required = true) @RequestHeader String Authorization,
                                                 @RequestBody ModifyBelongingInfoCondition condition){
        if (Objects.isNull(condition)) {
            throw new BusinessException("参数不合法");
        }
        if (condition.getOperatorId() == null) {
            throw new BusinessException("操作人为空");
        }
        if (CollectionUtils.isEmpty(condition.getDeptIdList())) {
            throw new BusinessException("所属组为空");
        }
        if (condition.getLegionId() == null) {
            throw new BusinessException("量子军团为空");
        }
        if (condition.getArmyId() == null) {
            throw new BusinessException("军团为空");
        }
        if (condition.getBusinessGroupId() == null) {
            throw new BusinessException("业务组ID为空");
        }
        groupManagerService.updateBelongingInfo(condition);
        return BusinessResult.createInstance(CommonResultMessage.SUCCESS);
    }

    @CheckLogin
    @PostMapping(value = "/rangeInfoPageList")
    @ApiOperation(value = "查询业务范围-分页" , notes = "查询业务范围-分页")
    public BusinessResult<PageResultVO<BusinessRangeInfo>> rangeInfoPageList(@ApiParam(value = "token", required = true) @RequestHeader String Authorization,
                                                                             @RequestBody RangeInfoCondition condition){
        if (Objects.isNull(condition)) {
            throw new BusinessException("参数不合法");
        }
        if (condition.getBusinessGroupId() == null) {
            throw new BusinessException("业务组ID为空");
        }
        return BusinessResult.createSuccessInstance(groupManagerService.rangePageList(condition));
    }

    @CheckLogin
    @PostMapping(value = "/belongingInfoPageList")
    @ApiOperation(value = "获取所有所属组" , notes = "获取所有所属组")
    public BusinessResult<PageResultVO<BusinessBelongingInfo>> belongingInfoPageList(@ApiParam(value = "token", required = true) @RequestHeader String Authorization,
                                                                                     @RequestBody BelongingInfoCondition condition){
        if (Objects.isNull(condition)) {
            throw new BusinessException("参数不合法");
        }
        if (condition.getBusinessGroupId() == null) {
            throw new BusinessException("业务组ID为空");
        }
        return BusinessResult.createSuccessInstance(groupManagerService.belongingInfoList(condition));
    }

    @CheckLogin
    @PostMapping(value = "/deletedBase")
    @ApiOperation(value = "删除业务组数据" , notes = "删除业务组数据")
    public BusinessResult businessGroupDeleted(@ApiParam(value = "token", required = true) @RequestHeader String Authorization,
                                            @RequestParam(value = "id") Long id){
        if (id == null) {
            throw new BusinessException("ID为空");
        }
        groupManagerService.deleteGroupBase(id ,Authorization);

        return BusinessResult.createInstance(CommonResultMessage.SUCCESS);
    }

    @CheckLogin
    @PostMapping(value = "/deletedRangeInfo")
    @ApiOperation(value = "删除业务范围" , notes = "删除业务范围")
    public BusinessResult rangeInfoDeleted(@ApiParam(value = "token", required = true) @RequestHeader String Authorization,
                                               @RequestParam(value = "id") Long id){
        if (id == null) {
            throw new BusinessException("业务范围ID为空");
        }
        groupManagerService.deletedRangeInfo(id , Authorization);

        return BusinessResult.createInstance(CommonResultMessage.SUCCESS);
    }

    @CheckLogin
    @PostMapping(value = "/deletedAllRange")
    @ApiOperation(value = "删除全部业务范围" , notes = "删除全部业务范围")
    public BusinessResult deletedAllRange(@ApiParam(value = "token", required = true) @RequestHeader String Authorization,
                                          @RequestBody RangeInfoCondition condition){
        if (condition == null) {
            throw new BusinessException("参数不合法");
        }
        if (condition.getBusinessGroupId() == null) {
            throw new BusinessException("业务组ID为空");
        }
        groupManagerService.deletedAllRangeInfo(condition,Authorization);

        return BusinessResult.createInstance(CommonResultMessage.SUCCESS);
    }

}
